package com.loan.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loan.dto.Result;
import com.loan.entity.Category;
import com.loan.entity.CategoryChannel;
import com.loan.entity.Channel;
import com.loan.repository.CategoryChannelRepository;
import com.loan.repository.CategoryRepository;
import com.loan.repository.ChannelRepository;
import com.loan.service.ChannelService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by huxudong on 17/7/6.
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private ChannelRepository channelRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private CategoryChannelRepository categoryChannelRepository;


    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public String hello(Model model) {
        return "index";
    }

    /**
     * 主页
     * @return
     */
    @RequestMapping(value = "main",method = RequestMethod.GET)
    public String main(Model model) {
        return "main";
    }



    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "channelList",method = RequestMethod.GET)
    public String channeList(Model model) {
        List<Channel> list = channelService.findByFlag(0);
        model.addAttribute("list", list);
        return "channel/list";
    }

    /**
     * 新增or编辑渠道
     * @return
     */
    @RequestMapping(value = "addChannel",method = RequestMethod.GET)
    public String addChannel(Model model,Long id) {
        List<Category> categorys = categoryRepository.findByFlagOrderBySort(0);

        List<Category> checked = new ArrayList<Category>();
        if(id != null){
            Channel channel = channelRepository.getOne(id);
            model.addAttribute("channel", channel);
            List<CategoryChannel> cc =channel.getCc();
            for(CategoryChannel categoryChannel : cc){
                checked.add(categoryChannel.getCategory());
            }
        }
        Collection<Category> unchecked = CollectionUtils.subtract(categorys, checked);
        JSONArray array = new JSONArray();
        for(Category check : checked){
            JSONObject o = new JSONObject();
            o.put("id",check.getId());
            o.put("name",check.getName());
            o.put("checked",true);
            array.add(o);
        }
        for(Category uncheck : unchecked){
            JSONObject o = new JSONObject();
            o.put("id",uncheck.getId());
            o.put("name",uncheck.getName());
            o.put("checked",false);
            array.add(o);
        }
        model.addAttribute("categorys",array);
        return "channel/add";
    }

    /**
     * 保存渠道
     * @return
     */
    @RequestMapping(value = "saveChannel",method = RequestMethod.POST)
    @ResponseBody
    public Result saveChannel(Channel channel, Long[] category ) {
        System.out.println(JSON.toJSONString(category));
        Result result = new Result(200,"添加成功");
        if(channel.getId() == null){
            channel.setaTime(new Date());
        }else{
            channel.setaTime(channelRepository.getOne(channel.getId()).getaTime());
        }
        channel.setcTime(new Date());
        if(!channel.getLogo().startsWith("http://")){
            channel.setLogo("http://123.56.70.231:9090"+channel.getLogo());
        }

        channelService.save(channel);

        categoryChannelRepository.deleteByChannelId(channel.getId());
        if(category != null && category.length > 0){
            for(Long categoryId : category){
                Category c = categoryRepository.getOne(categoryId);
                CategoryChannel cc = new CategoryChannel();
                cc.setaTime(new Date());
                cc.setcTime(new Date());
                cc.setChannel(channel);
                cc.setCategory(c);
                categoryChannelRepository.save(cc);
            }
        }
        return result;
    }

    /**
     * 删除渠道
     * @return
     */
    @RequestMapping(value = "deleteChannel",method = RequestMethod.POST)
    @ResponseBody
    public Result deleteChannel(Long id) {
        Result result = new Result(200,"添加成功");
        channelRepository.delete(id);
        return result;
    }

    /**
     * 设置渠道热门 1、热门 0、普通
     * @return
     */
    @RequestMapping(value = "updateChannelHot",method = RequestMethod.POST)
    @ResponseBody
    public Result updateChannelHot(Integer isHot , Long id) {
        Result result = new Result(200,"添加成功");
        channelService.updateHot(isHot,id);
        return result;
    }

}
